package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.entity.TaocanMaster;
import com.serkol.ams.mapper.TaocanMasterMapper;
import com.serkol.ams.service.TaocanDetailService;
import com.serkol.ams.service.TaocanMasterService;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author pingan
* @description 针对表【taocan_master】的数据库操作Service实现
* @createDate 2023-10-22 14:33:33
*/
@Service
public class TaocanMasterServiceImpl extends ServiceImpl<TaocanMasterMapper, TaocanMaster>
    implements TaocanMasterService{
    @Autowired
    private TaocanDetailService taocanDetailService;
    @Override
    public List<Map<String, Object>> findByList(long agentId) {
        MyLambdaQueryWrapper<TaocanMaster> queryWrapper=new MyLambdaQueryWrapper<>();

        queryWrapper.select(TaocanMaster::getId,TaocanMaster::getName,TaocanMaster::getRemark,TaocanMaster::getQuantity)
                        .select(TaocanMaster::getPaymentAmount).as("paymentAmount")
                        .select(TaocanMaster::getDiscountAmount).as("discountAmount")
                        .select(TaocanMaster::getTotalAmount).as("totalAmount")
                        .select(TaocanMaster::getAgentId).as("agentId")
                        .select(TaocanMaster::getRelationAgentId).as("relationAgentId")
                        .select(TaocanMaster::getIsActivty).as("isActivty")
                        .picture(TaocanMaster::getImageId).as("imageUrl");


        queryWrapper.eq(TaocanMaster::getRelationAgentId,agentId);

         List<Map<String, Object>> maps = this.listMaps(queryWrapper);
        for (int i = 0; i < maps.size(); i++) {
             Map<String, Object> stringObjectMap = maps.get(i);
            long taocanId = Long.parseLong(stringObjectMap.get("id") + "");
            List<Map<String,Object>> delist=taocanDetailService.findByDetailList(taocanId);
            stringObjectMap.put("details",delist);
            maps.set(i,stringObjectMap);
        }

        return maps;
    }
    @Override
    public MyPage<Map<String,Object>> findByPageList(int pageSize, int current) {
        MyLambdaQueryWrapper<TaocanMaster> queryWrapper=new MyLambdaQueryWrapper<>();

        queryWrapper.select(TaocanMaster::getId,TaocanMaster::getName,TaocanMaster::getRemark,TaocanMaster::getQuantity,TaocanMaster::getPaymentAmount,
               TaocanMaster::getDiscountAmount,
                TaocanMaster::getTotalAmount,
                TaocanMaster::getAgentId,
                TaocanMaster::getRelationAgentId, TaocanMaster::getIsActivty)
                .select("(SELECT NAME FROM agent WHERE ID=x.AGENT_ID LIMIT 1)").as("agentName")
                .select("(SELECT NAME FROM agent WHERE ID=x.RELATION_AGENT_ID LIMIT 1)").as("relationAgentName")
                .picture(TaocanMaster::getImageId).as("imageUrl");
        queryWrapper.orderByDesc(TaocanMaster::getId);
        Page<Map<String,Object>> page=new Page<>(current,pageSize);
        Page<Map<String,Object>> mapPage = this.baseMapper.selectMapsPage(page,queryWrapper);
        return MyPage.parsePage(mapPage);
    }

    @Override
    public List<Map<String, Object>> findByHomeList(long agentId) {
        MyLambdaQueryWrapper<TaocanMaster> queryWrapper=new MyLambdaQueryWrapper<>();
        queryWrapper.select(TaocanMaster::getId,TaocanMaster::getName,TaocanMaster::getRemark,TaocanMaster::getQuantity)
                .select(TaocanMaster::getPaymentAmount).as("paymentAmount")
                .select(TaocanMaster::getDiscountAmount).as("discountAmount")
                .select(TaocanMaster::getTotalAmount).as("totalAmount")
                .select(TaocanMaster::getAgentId).as("agentId")
                .select(TaocanMaster::getRelationAgentId).as("relationAgentId")
                .select(TaocanMaster::getIsActivty).as("isActivty")
                .picture(TaocanMaster::getImageId).as("imageUrl");


        queryWrapper.eq(TaocanMaster::getRelationAgentId,agentId);

        return this.listMaps(queryWrapper);
    }

    @Override
    public Map<String, Object> findById(long id) {

        MyLambdaQueryWrapper<TaocanMaster> queryWrapper=new MyLambdaQueryWrapper<>();

        queryWrapper.select(TaocanMaster::getId,TaocanMaster::getName,TaocanMaster::getRemark,TaocanMaster::getQuantity)
                .select(TaocanMaster::getPaymentAmount).as("paymentAmount")
                .select(TaocanMaster::getDiscountAmount).as("discountAmount")
                .select(TaocanMaster::getTotalAmount).as("totalAmount")
                .select(TaocanMaster::getAgentId).as("agentId")
                .select(TaocanMaster::getRelationAgentId).as("relationAgentId")
                .select(TaocanMaster::getIsActivty).as("isActivty")
                .picture(TaocanMaster::getImageId).as("imageUrl");


        queryWrapper.eq(TaocanMaster::getId,id);
        queryWrapper.last("LIMIT 1");

       Map<String, Object> map = this.getMap(queryWrapper);
 if(map!=null){
      Long taocanId =map.containsKey ("id")?(Long) map.getOrDefault("id", 0):0L;
     List<Map<String,Object>> delist=taocanDetailService.findByDetailList(taocanId);
     map.put("details",delist);
 }

        return map;
    }
}




